package com.cn.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cn.domain.Video;
import com.cn.domain.VideoOrder;
import com.cn.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 伍崇武
 * @date 2021-08-24 0024 - 下午 2:43
 */
@RestController
@RequestMapping("/api/v1/video_order")
// 实时刷新nacos配置文件
@RefreshScope
public class OrderController {

//    @Autowired
//    private RestTemplate restTemplate;
//
//    @Autowired
//    private DiscoveryClient discoveryClient;

    @Value("${video.title}")
    private String videoTitle;

    @Resource
    private VideoService videoService;

    @RequestMapping("/save")
    public Object save(Integer videoId){
        //Video video = restTemplate.getForObject("http://localhost:9000/api/v1/video/find_by_id?videoId="+videoId, Video.class);

        //List<ServiceInstance> instances = discoveryClient.getInstances("xdclass-video-service");

        //ServiceInstance serviceInstance = instances.get(0);

        // Video video = restTemplate.getForObject("http://xdclass-video-service/api/v1/video/find_by_id?videoId="+videoId, Video.class);

        Video video = videoService.findById(videoId);

        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setVideoId(video.getId());
        //videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setCreateTime(new Date());
        videoOrder.setVideoTitle(videoTitle);

        videoOrder.setServerInfo(video.getServerInfo());
        return videoOrder;
    }

    @PostMapping("/post")
    public Object postTest(@RequestBody Video video){
        Object obj = videoService.postTest(video);
        Map<String,Object> map = new HashMap<>();
        map.put("Video",obj);
        return map;
    }
    int temp = 0;
    @RequestMapping("/list")
    public Object list(HttpServletRequest request){
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        temp++;
//        if(temp%3 == 0){
//            throw new RuntimeException();
//        }
        Map<String,String> map = new HashMap<>();
        map.put("Video","obj");
        map.put("Video2","obj2");
        map.put("port",request.getServerPort()+"");
        return map;
    }
}
